<?php  

 class Users_db extends CI_Model {  

 	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	
	function getUser_All()
	/* version 0.1b Date : 06/04/2011
	 * Obtenir tous les utilisateurs..
	 * return 
	 * 	- name
	 *  - firstname
	 *  - mail
	 *  - phone
	 *  - country
	 */
	{
		$query = $this->db->query("SELECT * FROM blogs_users");
		return $query->result();
	}
	
	function getUser_Login($user_id)
	/* version 0.1b Date : 06/04/2011
	 * Obtenir le login d'un utilisateur.
	 * return 
	 * 	- login
	 */
	{
		$user_login;
		$query = $this->db->query("SELECT login FROM blogs_users WHERE id=$user_id");
		foreach ($query->result() as $user)
		{
			$user_login = $user->login;
		}
		return $user_login;
	}
	
	function getUser_Name($user_id)
	/* version 0.1b Date : 06/04/2011
	 * Obtenir le nom d'un utilisateur.
	 * return 
	 * 	- name
	 */
	{
		$user_name;
		$query = $this->db->query("SELECT name from blog_users WHERE id=$user_id");
		foreach ($query->result() as $user)
		{
			$user_name = $user->name;
		}
		return $user_name;
	}
	
	function getUser_firstName($user_id)
	/* version 0.1b Date : 06/04/2011
	 * Obtenir le prénom d'un utilisateur.
	 * return 
	 * 	- firstname
	 */
	{
		$user_firstname;
		$query= $this->db->query("SELECT firstname FROM blog_users WHERE id=$user_id");
		foreach ($query->result() as $user)
		{
			$user_firstname = $user->firstname;
		}
		return $user_firstname;
	}
	
 	function getUser_mail($user_id)
	/* version 0.1b Date : 06/04/2011
	 * Obtenir le mail d'un utilisateur.
	 * return 
	 * 	- mail
	 */
 	{
		$user_mail;
		$query= $this->db->query("SELECT mail FROM blog_users WHERE id=$user_id");
		foreach ($query->result() as $user)
		{
			$user_mail = $user->mail;
		}
		return $user_mail;
	}
	
 	function getUser_phone($user_id)
	/* version 0.1b Date : 06/04/2011
	 * Obtenir le numéro d'un utilisateur.
	 * return 
	 * 	- phone
	 */
 	{
		$user_phone;
		$query= $this->db->query("SELECT phone FROM blog_users WHERE id=$user_id");
		foreach ($query->result() as $user)
		{
			$user_phone = $user->phone;
		}
		return $user_phone;
	}

 	function getUser_country($user_id)
	/* version 0.1b Date : 06/04/2011
	 * Obtenir le pays d'un utilisateur.
	 * return 
	 * 	- country
	 */
 	{
		$user_country;
		$query= $this->db->query("SELECT country FROM blog_users WHERE id=$user_id");
		foreach ($query->result() as $user)
		{
			$user_country = $user->country;
		}
		return $user_country;
	}
		
	function validCredentials($username,$password)
	{
	    // $this->load->library('encrypt');
	
	    // $password = $this->encrypt->sha1($password);
	
	     //requ�te pr�par�e, beaucoup plus s�curis�
	     $q = "SELECT * FROM blog_users WHERE username = ? AND password = ?";
	
	     $data = array($username,$password);
	     $q = $this->db->query($q,$data);
	
	     if($q->num_rows() > 0)
	     {
	          $r = $q->result();
	          $session_data = array('level' =>$r[0]->level, 'username' => $r[0]->username,'logged_in' => true);
	          $this->session->set_userdata($session_data);
	          return true;
	     } 
	     
	     else 
	     { 
	     	return false; 
	     }
	}
	
	function isLoggedIn()
	{
	     if($this->session->userdata('logged_in'))
	     { 
	     	return true; 
	     } 
	     else
	     { 
	      	return false;
	     }
	}

	function setUser_account($login,$password,$mail)
	/* version 0.1b Date : 06/04/2011
	 * Crée un nouveau utilisateur.
	 * argument
	 * 	- login
	 *  - password
	 *  - mail
	 */
	{
		$user_account = array('login' => $login,
							'password' => $password,
							'mail' => $mail
						   );
		$this->db->insert('blog_users', $user_account);
	}

	function setUser_mail($user_id,$mail)
	{
		
	}
	
 	function logOff()
	{
		$session = array('logged_in' => '','username'=>'','level'=>'');
		if($this->session->unset_userdata($session))
		{
			return TRUE;
		}
	}
 }  

 ?> 
